package zheezes.protocol.impl.gbw.format;

import java.math.BigDecimal;
import java.util.Arrays;

import zheezes.util.ArrayUtil;
import zheezes.util.format.NumberFormat;


public class DataForm13 extends DataForm {
	public static final int SIZE = 4;
	
	private BigDecimal value = null;

	@Override
	public int sizeof() {
		return SIZE;
	}

	@Override
	public int getBytes(byte[] buf, int offset) {
		int idx = offset;
		if (value != null) {
			buf[idx++] = NumberFormat.getByte(value, NumberFormat.DIMI);
			buf[idx++] = NumberFormat.getByte(value, NumberFormat.MICRO);
			buf[idx++] = NumberFormat.getByte(value, NumberFormat.UNIT);
			buf[idx++] = NumberFormat.getByte(value, NumberFormat.HUND);
		} else {
			Arrays.fill(buf, idx, idx + 4, (byte) 0xEE);
			idx += 4;
		}

		return idx - offset;
	}

	@Override
	public int setBytes(byte[] buf, int offset) {
		int idx = offset;
		if (ArrayUtil.count(buf, idx, (byte) 0xEE) < 4) {
			value = new BigDecimal("0");
			value = NumberFormat.setByte(value, NumberFormat.DIMI, buf[idx++]);
			value = NumberFormat.setByte(value, NumberFormat.MICRO, buf[idx++]);
			value = NumberFormat.setByte(value, NumberFormat.UNIT, buf[idx++]);
			value = NumberFormat.setByte(value, NumberFormat.HUND, buf[idx++]);
		} else {
			value = null;
			idx += 4;
		}

		return idx - offset;
	}

	public BigDecimal getValue() {
		return value;
	}
}
